package controllers

import (
	"db2_ddl_search/service"
	"net/http"
	"strconv"

	"github.com/gin-gonic/gin"
)

func GetDbObject(context *gin.Context) {
	//节点类型
	nodetype := context.Param("type")

	//模式
	scema := context.Param("scema")

	//对象名
	objectname := context.Param("objectname")

	ret, err := service.GetObjectsBySchemeAndTypeAndName(scema, nodetype, objectname)

	if err == nil && ret != nil {
		context.JSON(http.StatusOK, ret)

		return
	}
}

//搜索对象
func SearchDbObjects(context *gin.Context) {
	//页数
	page := context.PostForm("page")
	ipage, _ := strconv.Atoi(page)

	//每页行数
	pageSize := context.PostForm("pageSize")

	ipageSize, _ := strconv.Atoi(pageSize)

	//查询对象名
	queryString := context.PostForm("queryString")
	list, total, err := service.GetObjectsByQueryString(queryString, ipage, ipageSize)

	if err == nil {

		ret := make(map[string]interface{})
		ret["totalElements"] = total
		ret["content"] = list

		context.JSON(http.StatusOK, ret)

		return
	}

}
//刷新索引
func SaveEsIndex(context *gin.Context) {
    service.SaveEsIndex()
    context.JSON(http.StatusOK, "ok")
}
